<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PagesController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Pages';

/**
 * Default helper
 *
 * @var array
 */
	public $helpers = array('Html', 'Session');

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array();
/**
 * Default components
 * 
 * @var array
 */
    public $components = array('Email');
    
    public function beforeFilter() {
        parent::beforeFilter();
       // $this->Auth->allowedActions = array('display','contact','faq','testimony','sample', 'admin_index');
        $this->Auth->allow('*');
    }

    /**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 */
	public function display() {             
		$path = func_get_args();

		$count = count($path);                
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title_for_layout = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
                $this->loadModel('Article');                
                $testimonies = $this->Article->find('all',array(
                        'conditions' => array(
                            'Article.type' => ARTICLE_TYPE_TESTIMONIAL,
                            'Article.status' => 1
                            ),
                        'contain' => array('User'),
                        'order' => array(
                            'Article.id' => 'DESC'
                            )
                    ));
                if($this->Auth->user('id')){
                    $testimonies2 = $this->Article->find('all',array(
                            'conditions' => array(
                                'Article.type' => ARTICLE_TYPE_TESTIMONIAL,
                                'Article.status' => 0,
                                'Article.user_id' => $this->Auth->user('id')
                                ),
                            'contain' => array('User'),
                            'order' => array(
                            'Article.id' => 'DESC'
                            )
                        ));
                    $testimonies = array_merge($testimonies,$testimonies2);                    
                    $this->set('users', $this->Auth->user());
                }                
                $this->set('testimonies',$testimonies);
		$this->set(compact('page', 'subpage', 'title_for_layout'));
		$this->render(implode('/', $path));
	}
        public function contact() {                    
            $this->setPageTitle(__('Contact Us'));
            if(!empty($this->data)){
                $defaults = array(
                'to' => Configure::read('App.defaultEmail'),
                'subject' => 'User Contact: ' .$this->data['ContactUs']['subject'],
                'template' => 'contact',
                'layout' => 'default');                
                $msg = $this->data['ContactUs']['message'];                            
                $options = $defaults;
                $Email = $this->_getMailInstance();         //truetexttranslation@gmail.com
                $Email->to($options['to'])
                        ->from($this->data['ContactUs']['email'])
                        ->subject($options['subject'])
                        ->template($options['template'], $options['layout'])
                        ->viewVars(array(   
                            'email' => $this->data['ContactUs']['email'],
                            'name' => $this->data['ContactUs']['name'],
                            'phone' => $this->data['ContactUs']['phone'],
                            'msg' => $msg
                            ))
                        ->send();
                /*
                $Email->to($this->data['ContactUs']['email'])
                        ->from($options['to'])
                        ->subject("Thank for your contact")                                                
                        ->send('Hello ' . $this->data['ContactUs']['name'] . "\n\nThanks for your contact us.\nWe will contact you back as soon as possible.");
                $this->request->data = null;
                 * 
                 */
                $this->redirect(array('controller' => 'pages', 'action' => 'contactSuccess'));
            }
            if($this->Auth->user('id')){                    
                $this->set('users', $this->Auth->user());
            }
        }
        
        public function contactSuccess() {
            $this->setPageTitle(__('Contact Success'));
            //contact success do not delete this action
        }
        public function faq(){
            $this->setPageTitle(__('FAQ'));
            if(!empty($this->data)){
                
            }
            if($this->Auth->user('id')){                    
                $this->set('users', $this->Auth->user());
            }
        }        
        public function about() {
            
            $this->setPageTitle(__('About True News World'));
	}
        public function service() {
            $this->setPageTitle(__('Our Service'));
	}
        public function search() {
            $this->setPageTitle(__('Search'));
        }
        
        public function term() {
            $this->setPageTitle(__('Terms and Conditions'));
        }
        /**
         * ADMIN SECTION
         */
        public function admin_index() {
            $this->setPageTitle(__('Dashboard'));
            $this->loadModel('User');
            $all_active_subscribes = $this->User->getAllActiveSubscribers();
            $last_subscribes = $this->User->getLastSubscribers(5);
            $total_article_for_subscribes = $this->User->getTotalArticleForSubscribers();
            $subscribe_before_expries = $this->User->getSubscribersBeforeExpire();
            $this->set(compact('all_active_subscribes','last_subscribes','total_article_for_subscribes','subscribe_before_expries'));
        }
        /**
         * Company infomation 
         */
        public function admin_company() {
            $this->loadModel('Setting');
            $company = $this->Setting->getCompany();
            $this->set('company', $company);
	}
        public function admin_companyEdit() {
             $this->loadModel('Setting');
            $company = $this->Setting->getCompany();
            if ($this->request->is('post')) {
                $this->Setting->setCompany($this->request->data['Page']);
                $this->redirect(array('action' => 'company'));
            }
            $this->set('company', $company);
        }
    /**
     * Returns a CakeEmail object
     *
     * @return object CakeEmail instance
     * @link http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
     */
    protected function _getMailInstance() {
        App::uses('CakeEmail', 'Network/Email');
        return new CakeEmail();
    }
}

